﻿/// <reference path="jquery-1.4.1.js" />
$(function () {
    var numToChinese = {
        "1": { 'prevClass': "", 'margin_top': '50px', 'margin_left': '40px', money: 500, points: 1 },
        "2": { 'prevClass': "second", 'margin_top': '55px', 'margin_left': '0', money: 2000, points: 4 },
        "3": { 'prevClass': "third", 'margin_top': '70px', 'margin_left': '5px', money: 8000, points: 16 },
        "4": { 'prevClass': "fourth", 'margin_top': '85px', 'margin_left': '10px', money: 32000, points: 64 },
        "5": { 'prevClass': "fifth", 'margin_top': '85px', 'margin_left': '0', money: 128000, points: 256 },
        "6": { 'prevClass': "sixth", 'margin_top': '77px', 'margin_left': '0', money: 512000, points: 1024 },
        "7": { 'prevClass': "seventh", 'margin_top': '67px', 'margin_left': '0', money: 2048000, points: 4096 }
    };
    var firstClick = true;
    var startGame = true;
    var gainAward = false;
    $("#btn_beginGame").click(function () {
        if (startGame) {
            startGame = false;
            var btn_beginGame = $(this);
            btn_beginGame.addClass("goon").hide();
            $("p.tips").show();
            $("ul.game li").each(function () {
                if ($(this).index() == 0) {
                    $(this).addClass("started");
                } else {
                    var index = numToChinese[$(this).index() + 1].prevClass;
                    $(this).removeClass(index).addClass(index + "-started");
                }
            }).click(function () {
                if (firstClick) {
                    btn_beginGame.show();
                    $("p.tips").hide();
                    firstClick = false;
                }
                if ($(this).attr("class").indexOf('checked') == -1) {
                    DrawAward($(this));
                }
            });
        }
        if (gainAward) {
            $.post('ajax.ashx', { 'action': 'gainAward' }, function (data) {
                data = data.split(',');
                GainAward(data[0]);
                ResetGame(data[1]);
            });
        }

        gainAward = true;
    });
    (function () {
        GetRankList();
        GetUserUsablePropCounts();
        GetUserState();
    })();

    function GetUserState() {
        $.post("ajax.ashx", { "action": 'getuserstate' }, function (data) {
            if (!data || data.length == 0) { return; }
            $("#btn_beginGame").click().addClass('goon').show();
            $("p.tips").hide();
            $("p.total").html("{0}个葫芦娃：价值{1}币".format(data.length, numToChinese[data.length].money));
            $("ul.game li").each(function () {
                var li = $(this);
                $.each(data, function (key, value) {
                    var index = li.index() + 1;
                    var number = value.number;
                    if (number == index) {
                        var item = numToChinese[number];
                        var classPrev = numToChinese[number].prevClass;
                        li.attr('class', '').addClass("{0}{1}{2}".format(classPrev, number == 1 ? "" : "-", "checked")).css({ 'margin-top': item.margin_top, 'margin-left': item.margin_left });
                    }
                });
            });
        });
    }
    function GainAward(money) {
        if (money <= 0) { money = 0; }
        alert('恭喜您获得{0}币'.format(money));
    }
    function DrawAward(elm) {
        $.post("ajax.ashx", { "action": 'drawAward' }, function (data) {
            if (!data) { return; }
            switch (data.Code) {
                case 1:
                    alert(data.Msg);
                    ResetGame(data.ReturnResult.usablePropCounts);
                    return;
                case 2:
                    var item = numToChinese[elm.index() + 1];
                    var classPrev = numToChinese[data.ReturnResult.number].prevClass;
                    elm.attr('class', '').addClass("{0}{1}{2}".format(classPrev, data.ReturnResult.number == 1 ? "" : "-", "checked")).css({ 'margin-top': item.margin_top, 'margin-left': item.margin_left });
                    alert(data.Msg);
                    ResetGame(data.ReturnResult.usablePropCounts);
                    return;
                case 3:
                    $("ul.game li").unbind('click');
                    break;
                default:
                    alert(data);
                    ResetGame();
                    return;
                    break;
            }
            $("p.total").html("{0}个葫芦娃：价值{1}币".format(data.ReturnResult.counts, data.ReturnResult.money));
            $("#usablePropCounts").html(data.ReturnResult.usablePropCounts);
            var item = numToChinese[elm.index() + 1];
            var classPrev = numToChinese[data.ReturnResult.number].prevClass;
            elm.attr('class', '').addClass("{0}{1}{2}".format(classPrev, data.ReturnResult.number == 1 ? "" : "-", "checked")).css({ 'margin-top': item.margin_top, 'margin-left': item.margin_left });
            $("#usablePropCounts").html(data.ReturnResult.usablePropCounts);
            alert(data.Msg);
        });
    }
    function ResetGame(usablePropCounts) {
        startGame = true;
        firstClick = true;
        gainAward = false;
        $("ul.game li").each(function () {
            var index = $(this).index();
            var prevClass = numToChinese[index + 1].prevClass;
            $(this).attr('class', '');
            $(this).addClass(prevClass);
        });
        $("p.total").html("0个葫芦娃：价值0币");
        $("#usablePropCounts").html(usablePropCounts);
        $("#btn_beginGame").removeClass('goon');
        $("ul.game li").unbind('click');
    }
    function GetUserUsablePropCounts() {
        $.get('ajax.ashx', { "r": Math.random(), "action": "GetUserUsablePropCounts" }, function (data) {
            $("#usablePropCounts").html(data);
        });
    }
    function GetRankList() {
        $.get('ajax.ashx', { "r": Math.random(), "action": "getranklist" }, function (data) {
            if (!data || data.length <= 0) { return; }
            var htmlTemplate = "<tr><td>{0}</td><td><span>{1}</span></td><td>{2}</td></tr>";
            var htmlArr = [];
            htmlArr.push("<tr width='33'><th>排名</th><th width='118'><span>昵称</span></th><th width='60'>积分</th></tr>");
            $.each(data, function (key, value) {
                htmlArr.push(htmlTemplate.format(key + 1, value.nickname, value.points));
            });
            var tb = $("div.ranking table");
            tb.children().remove();
            tb.append($(htmlArr.join('')));
        });
    }
    String.prototype.format = function () {
        var args = arguments;
        var reg = /{(\d+)?}/g;
        return this.replace(reg, function ($0, $1) { return args[parseInt($1)]; })
    };
});